home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / genra_tree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  10.3 KB  |  383 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  genra_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_GENRA_TREE_H
  16. #define LEDA_GENRA_TREE_H
  17.  
  18. // macros for counting 
  19. //
  20. #ifdef COUNT
  21. #define COUNT_SEC(l)    genra_tree::sec_op[last_op]+=l;
  22. #define COUNT_INS     genra_tree::prim_op[0]++; genra_tree::last_op=0;
  23. #define COUNT_DEL    genra_tree::prim_op[1]++; genra_tree::last_op=1;
  24. #define COUNT_VARS    static int prim_op[2],sec_op[2],last_op;
  25. #define COUNT_FUNC    float analyze_del(); float analyze_ins();\
  26.             void reset();
  27. #else
  28. #define COUNT_SEC(l)
  29. #define COUNT_INS
  30. #define COUNT_DEL
  31. #define COUNT_VARS
  32. #define COUNT_FUNC    void analyze() {} ;  void reset() {} ;
  33. #endif
  34.  
  35.  
  36.  
  37. #include <LEDA/list.h>
  38.  
  39. //
  40. // D-Dimensional Generic Range Trees
  41. // ---------------------------------
  42. //
  43. //
  44. // exchangable underlying data structure
  45. //
  46. // 7/92, M.Paul
  47. //
  48.  
  49.  
  50.  
  51. // an element of a genra_tree stores key and inf
  52. //
  53. class grt_inf
  54. {
  55.   friend class genra_tree ;
  56.  
  57.   GenPtr* g_key ;
  58.   GenPtr g_inf ;
  59.  
  60.   public:
  61.  
  62.     LEDA_MEMORY( grt_inf ) ;            // better proteced
  63.  
  64.     grt_inf( GenPtr k[], GenPtr i ) { g_key=k; g_inf=i; }
  65.  
  66.     GenPtr*& key() { return g_key; } ;
  67.     GenPtr key( int d ) { return g_key[d]; } ;
  68.     GenPtr& inf() { return g_inf; } ;
  69. } ;
  70.  
  71. typedef grt_inf* grt_item ;
  72.  
  73.  
  74.  
  75. #include <LEDA/impl/rs_tree.h>            // include default UDS
  76. typedef rs_tree def_uds ;
  77.  
  78.  
  79.  
  80. // genra_tree is a generic d-dimensional range tree with default
  81. // underlying data structure def_uds
  82. //
  83. class genra_tree : public def_uds
  84. {
  85.   COUNT_VARS
  86.   static grt_item GI_L, GI_H ;    
  87.  
  88.   // functions used from underlying data structure are virtual
  89.  
  90.   virtual GenPtr r_clear() { return def_uds::r_clear(); } ;
  91.   virtual int r_size() const { return def_uds::r_size(); } ;
  92.   virtual GenPtr r_first_item() const 
  93.     { return def_uds::r_first_item(); } ;
  94.   virtual GenPtr r_next_item( GenPtr p ) const 
  95.     { return def_uds::r_next_item(p); } ;
  96.   virtual GenPtr r_inf( GenPtr p ) const { return def_uds::r_inf(p); } ;
  97.   virtual void r_partition( GenPtr low, GenPtr high, 
  98.                 list<GenPtr>& node_infs, list<GenPtr>& leaf_infs ) const 
  99.     { def_uds::r_partition(low,high,node_infs,leaf_infs); } ;
  100.   virtual void r_query( GenPtr low, GenPtr high, list<GenPtr>& infs ) const
  101.     { def_uds::r_query(low,high,infs); } ;
  102.   virtual void r_leaf_infs( GenPtr p, list<GenPtr>& infs ) const
  103.     { def_uds::r_leaf_infs(p,infs); } ;
  104.   virtual list<GenPtr> r_all_leaf_infs() const
  105.     { return def_uds::r_all_leaf_infs(); } ;
  106.   virtual void r_insert( GenPtr key, GenPtr leaf_inf, GenPtr inner_inf,
  107.                 list<GenPtr>& cn, list<GenPtr>& in )
  108.     { def_uds::r_insert(key,leaf_inf,inner_inf,cn,in); } ;
  109.   virtual GenPtr r_delete( GenPtr key, list<GenPtr>& cn, list<GenPtr>& dn )
  110.     { return def_uds::r_delete(key,cn,dn); } ;
  111.  
  112.   protected:
  113.  
  114.     LEDA_MEMORY( genra_tree ) ;
  115.  
  116.     int dim1 ;            // one less than the dimension of tree
  117.  
  118.     virtual genra_tree* create_genra_tree( int d )
  119.       { return new genra_tree(d); } ;
  120.  
  121.     // virtual functions of uds
  122.  
  123.     void print_key( GenPtr ) const ;
  124.     void print_inf( GenPtr ) const ;
  125.     int cmp( GenPtr, GenPtr ) const ;
  126.     void clear_key(GenPtr&) const {}
  127.     void clear_inf(GenPtr&) const {}
  128.     void copy_key(GenPtr&) const {}
  129.     void copy_inf(GenPtr&) const {}
  130.     int int_type() const { return 0; }
  131.  
  132.     // user defined virtual functions of class genra_tree
  133.  
  134.     virtual int r_cmp( GenPtr x, GenPtr y ) const
  135.       { return compare(x,y); } ;
  136.     virtual void r_copy_key( GenPtr*& ) const {}
  137.     virtual void r_copy_inf( GenPtr& ) const {} 
  138.     virtual void r_clear_key( GenPtr*& ) const {}
  139.     virtual void r_clear_inf( GenPtr& ) const {}
  140.  
  141.     int is_between( grt_item k_gi, grt_item l_gi, grt_item h_gi ) const;
  142.     void query( grt_item l_gi, grt_item h_gi, list<GenPtr>& lgi ) const ;
  143.     void insert_item( grt_item gi ) ;
  144.     void del_item( grt_item gi ) ;
  145.     void build_tree( list<GenPtr>& lgi ) ;
  146.  
  147.   public:
  148.  
  149.     virtual void r_print_key( GenPtr* x ) const 
  150.       { for( int d=dim1; d>=0; d-- ) cout << x[d] << " "; }
  151.     virtual void r_print_inf( GenPtr i ) const { cout << i << " "; }
  152.     
  153.     virtual void print_tree(){ def_uds::print_tree(); } ;
  154.     void print() ;
  155.     void test() ;
  156.  
  157.     genra_tree( int d = 2 ) { dim1 = d-1; }
  158.  
  159.     void clear() ;
  160.     virtual ~genra_tree() { clear(); }
  161.  
  162.     int dimension() const { return dim1+1; }
  163.     int size() const { return r_size(); }
  164.     int empty() const { return r_size()==0; }
  165.     void query( GenPtr low[], GenPtr high[], list<GenPtr>& lgi ) const ;
  166.     grt_item lookup( GenPtr key[] ) const ;
  167.     list<GenPtr> all_items() const ;
  168.  
  169.     grt_item insert( GenPtr key[], GenPtr inf ) ;
  170.     void del( GenPtr key[] ) ;
  171.  
  172.     COUNT_FUNC
  173. } ;
  174.  
  175. typedef genra_tree* grt_p ;
  176.  
  177.  
  178.  
  179. //
  180. // some inline functions of class genra_tree
  181. //
  182.  
  183. // returns a list of all grt_items with key between low[] and high[]
  184. //
  185. inline void genra_tree::query( GenPtr low[], GenPtr high[],
  186.                    list<GenPtr>& lgi ) const 
  187. {
  188.   lgi.clear() ;
  189.  
  190.   if( ! empty() ) {
  191.     GI_L->key() = low ;  GI_H->key() = high ;
  192.     query( GI_L, GI_H, lgi ) ;
  193.   }
  194. }
  195.  
  196.  
  197.  
  198. // returns a list of all grt_items
  199. //
  200. inline list<GenPtr> genra_tree::all_items() const 
  201. {
  202.   return r_all_leaf_infs() ;
  203. }
  204.  
  205.  
  206.  
  207. // insert inf with key[] or change inf, if key[] exists
  208. //
  209. inline grt_item genra_tree::insert( GenPtr key[], GenPtr inf ) 
  210. {
  211.   grt_item gi = lookup( key ) ;            // search for key[]
  212.   
  213.   if( ! gi ) {                    // insert new item
  214.     gi = new grt_inf( key, inf ) ;  
  215.     r_copy_inf( gi->inf() ) ;  r_copy_key( gi->key() ) ;  
  216.     COUNT_INS
  217.     insert_item( gi ) ;                
  218.   }
  219.   else {                    // change inf
  220.     // r_clear_inf( gi->inf() ) ;
  221.     // gi->inf() = inf ;  
  222.     // r_copy_inf( gi->inf() ) ;
  223.   }
  224.  
  225.   return gi ;
  226. } ;
  227.  
  228.  
  229.  
  230. // delete inf with key[]
  231. //
  232. inline void genra_tree::del( GenPtr key[] )
  233. {
  234.   grt_item gi = lookup( key ) ;                 // search for key[]
  235.  
  236.   if( gi ) {
  237.     COUNT_DEL
  238.     del_item( gi ) ;
  239.     r_clear_inf( gi->inf() ) ;  r_clear_key( gi->key() ) ;  
  240.     delete gi ;
  241.   }
  242. } ;
  243.  
  244.  
  245.  
  246. // checks if k_gi[] lies between l_gi[] and h_gi[] for all dims < dim1
  247. //
  248. inline int genra_tree::is_between( grt_item k_gi, 
  249.                               grt_item l_gi, grt_item h_gi ) const
  250. {
  251.   register int d = dim1 ;
  252.  
  253.   while( --d >= 0 ) 
  254.     if( r_cmp(k_gi->key(d),l_gi->key(d)) < 0 || 
  255.     r_cmp(k_gi->key(d),h_gi->key(d)) > 0 ) 
  256.       break ;
  257.  
  258.   return d<0 ? 1 : 0 ;
  259. }
  260.  
  261.  
  262.  
  263. //
  264. // virtual functions for uds
  265. //
  266.  
  267. inline void genra_tree::print_key( GenPtr x ) const
  268. { r_print_key( grt_item(x)->key() ) ;
  269. }
  270.  
  271. inline void genra_tree::print_inf( GenPtr x ) const
  272. { r_print_inf( grt_item(x)->inf() ) ;
  273. }
  274.  
  275. // use key in dim1 first, then take inf
  276. //
  277. inline int genra_tree::cmp( GenPtr x, GenPtr y ) const
  278. {
  279.   int c = r_cmp( grt_item(x)->key(dim1), grt_item(y)->key(dim1) ) ;
  280.   return c ? c : compare( (GenPtr) grt_item(x)->inf(), 
  281.               (GenPtr) grt_item(y)->inf() ) ;
  282. }
  283.  
  284.  
  285.  
  286. //
  287. // add capability of exchangable underlying data structure
  288. //
  289.  
  290. #define GENRA_TREE(a)    name2(a,_GENRA_TREE)
  291.  
  292.  
  293.  
  294. #define GENRA_TREE_DECL(impl)\
  295. \
  296. class GENRA_TREE(impl) : public genra_tree, public impl\
  297. {\
  298. \
  299.   GenPtr r_clear() { return impl::r_clear(); }\
  300.   int r_size() const { return impl::r_size(); }\
  301.   GenPtr r_first_item() const { return impl::r_first_item(); }\
  302.   GenPtr r_next_item( GenPtr p ) const \
  303.     { return impl::r_next_item(p); }\
  304.   GenPtr r_inf( GenPtr p ) const { return impl::r_inf(p); }\
  305.   void r_partition( GenPtr low, GenPtr high,\
  306.             list<GenPtr>& node_infs, list<GenPtr>& leaf_infs ) const \
  307.     { impl::r_partition(low,high,node_infs,leaf_infs); }\
  308.   void r_query( GenPtr low, GenPtr high, list<GenPtr>& infs ) const\
  309.     { impl::r_query(low,high,infs); }\
  310.   void r_leaf_infs( GenPtr p, list<GenPtr>& infs ) const\
  311.     { impl::r_leaf_infs(p,infs); }\
  312.   list<GenPtr> r_all_leaf_infs() const\
  313.     { return impl::r_all_leaf_infs(); }\
  314.   void r_insert( GenPtr key, GenPtr leaf_inf, GenPtr inner_inf,\
  315.          list<GenPtr>& cn, list<GenPtr>& in )\
  316.     { impl::r_insert(key,leaf_inf,inner_inf,cn,in); }\
  317.   GenPtr r_delete( GenPtr key, list<GenPtr>& cn, list<GenPtr>& dn )\
  318.     { return impl::r_delete(key,cn,dn); }\
  319. \
  320.   protected:\
  321. \
  322.     LEDA_MEMORY( GENRA_TREE(impl) ) ;\
  323. \
  324.     genra_tree* create_genra_tree( int d )\
  325.       { return new GENRA_TREE(impl)(d); } ;\
  326. \
  327.     void print_key( GenPtr x ) const { genra_tree::print_key(x); }\
  328.     void print_inf( GenPtr x ) const  { genra_tree::print_inf(x); }\
  329.     int cmp( GenPtr x, GenPtr y ) const\
  330.       { return genra_tree::cmp(x,y); }\
  331.     void clear_key(GenPtr&) const {}\
  332.     void clear_inf(GenPtr&) const {}\
  333.     void copy_key(GenPtr&) const {}\
  334.     void copy_inf(GenPtr&) const {}\
  335.     int int_type() const { return 0 ; }\
  336. \
  337.     int r_cmp( GenPtr x, GenPtr y ) const { return compare(x,y); }\
  338.     void r_copy_key( GenPtr*& ) const {}\
  339.     void r_copy_inf( GenPtr& ) const {}\
  340.     void r_clear_key( GenPtr*& ) const {}\
  341.     void r_clear_inf( GenPtr& ) const {}\
  342. \
  343.     void query( grt_item l_gi, grt_item h_gi, list<GenPtr>& lgi ) const \
  344.       { genra_tree::query(l_gi,h_gi,lgi); }\
  345.     int is_between( grt_item k_gi, grt_item l_gi, grt_item h_gi ) const\
  346.       { return genra_tree::is_between(k_gi,l_gi,h_gi); }\
  347.     void insert_item( grt_item gi ){ genra_tree::insert_item(gi); }\
  348.     void del_item( grt_item gi ){ genra_tree::del_item(gi); }\
  349.     void build_tree( list<GenPtr>& lgi ) { genra_tree::build_tree(lgi); }\
  350. \
  351.   public:\
  352. \
  353.     virtual void r_print_key( GenPtr* x ) const\
  354.       { for( int d=dim1; d>=0; d-- ) cout << x[d] << " "; }\
  355.     virtual void r_print_inf( GenPtr i ) const { cout << i << " "; }\
  356. \
  357.     void print_tree(){ impl::print_tree(); } ;\
  358.     void print(){ genra_tree::print(); } ;\
  359. \
  360.     GENRA_TREE(impl)( int d = 2 ) { dim1 = d-1; }\
  361.     virtual ~GENRA_TREE(impl)() { clear(); }\
  362. \
  363.     void clear() { genra_tree::clear(); }\
  364.     int dimension() const { return genra_tree::dimension(); }\
  365.     int size() const { return genra_tree::size(); }\
  366.     int empty() const { return genra_tree::empty(); }\
  367.     void query( GenPtr low[], GenPtr high[], list<GenPtr>& lgi ) const\
  368.       { genra_tree::query(low,high,lgi); }\
  369.     grt_item lookup( GenPtr key[] ) const\
  370.       { return genra_tree::lookup(key); }\
  371.     list<GenPtr> all_items() const { return genra_tree::all_items(); }\
  372. \
  373.     grt_item insert( GenPtr key[], GenPtr inf )\
  374.       { return genra_tree::insert(key,inf); }\
  375.     void del( GenPtr key[] ) { genra_tree::del(key); }\
  376. \
  377. } ;\
  378.  
  379.  
  380.  
  381. #endif
  382.